package com.example.tliasfight.controller;

import com.example.tliasfight.anno.Mylog;
import com.example.tliasfight.pojo.Character;
import com.example.tliasfight.pojo.Emp;
import com.example.tliasfight.pojo.PageBean;
import com.example.tliasfight.pojo.Result;
import com.example.tliasfight.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
public class EmpController
{
    @Autowired
    private EmpService empService;

//    public Result list2(@ModelAttribute Character character)
//    {
//
//        empService.page2(character);
//
//       return Result.success();
//
//    }
   @GetMapping(value ="/emps")
    public Result list(Integer page , Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end)
    {

        if(page==null)page=1;
        if(pageSize==null)pageSize=10;
        PageBean pageBean = empService.Page(page,pageSize, name,gender,begin,end);
        return Result.success(pageBean);

//        自己的尝试
//        if(character.getPage()==null)character.setPage(1);
//        if(character.getPageSize()==null)character.setPageSize(10);
//        PageBean pageBean = empService.list(character);
//        return Result.success(pageBean);
    }
    @DeleteMapping(value = "/emps/{ids}")
    @Mylog
    public Result delete(@PathVariable List<Integer> ids)
    {
        empService.delete(ids);
        return Result.success();
    }

    @PostMapping(value = "/emps")
    @Mylog
    public Result addEmp(@RequestBody Emp emp)
    {
        empService.addEmp(emp);
        return Result.success();
    }
    @GetMapping(value = "/emps/{id}")
    public Result getById(@PathVariable Integer id)
    {
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }
    @PutMapping("/emps")
    @Mylog
     public Result update(@RequestBody Emp emp)
    {
          empService.update(emp);
        return Result.success();
    }
}
